<?php
/**
 * Created by PhpStorm.
 * User: muhe0
 * Date: 2017/4/18
 * Time: 1:55
 */

namespace LDL\Twspider\E;
//http://www.weicot.com/php-%E6%88%96%E5%85%B6%E4%BB%96%E8%84%9A%E6%9C%AC-%E5%9C%A8%E7%BB%88%E7%AB%AF%E8%BE%93%E5%87%BA%E5%B8%A6%E9%A2%9C%E8%89%B2%E7%9A%84%E5%AD%97%E4%BD%93-%E6%88%96%E5%88%B6%E8%A1%A8%E7%AC%A6/

use Symfony\Bridge\Monolog\Logger;
use Symfony\Component\Console\Logger\ConsoleLogger;

class Colors {
    private $foreground_colors = array();
    private $background_colors = array();

    public function __construct() {
        // Set up shell colors
        $this->foreground_colors['black'] = '0;30';
        $this->foreground_colors['dark_gray'] = '1;30';
        $this->foreground_colors['blue'] = '0;34';
        $this->foreground_colors['light_blue'] = '1;34';
        $this->foreground_colors['green'] = '0;32';
        $this->foreground_colors['light_green'] = '1;32';
        $this->foreground_colors['cyan'] = '0;36';
        $this->foreground_colors['light_cyan'] = '1;36';
        $this->foreground_colors['red'] = '0;31';
        $this->foreground_colors['light_red'] = '1;31';
        $this->foreground_colors['purple'] = '0;35';
        $this->foreground_colors['light_purple'] = '1;35';
        $this->foreground_colors['brown'] = '0;33';
        $this->foreground_colors['yellow'] = '1;33';
        $this->foreground_colors['light_gray'] = '0;37';
        $this->foreground_colors['white'] = '1;37';

        $this->background_colors['black'] = '40';
        $this->background_colors['red'] = '41';
        $this->background_colors['green'] = '42';
        $this->background_colors['yellow'] = '43';
        $this->background_colors['blue'] = '44';
        $this->background_colors['magenta'] = '45';
        $this->background_colors['cyan'] = '46';
        $this->background_colors['light_gray'] = '47';
    }

    // Returns colored string
    public function getColoredString($string, $foreground_color = null, $background_color = null) {
        $colored_string = "";

        // Check if given foreground color found
        if (isset($this->foreground_colors[$foreground_color])) {
            $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
        }
        // Check if given background color found
        if (isset($this->background_colors[$background_color])) {
            $colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
        }
	
        // Add string and end coloring
	
	$colored_string .=  !empty($colored_string) ? $string . "\033[0m" : $string;

        return $colored_string;
    }

    // Returns all foreground color names
    public function getForegroundColors() {
        return array_keys($this->foreground_colors);
    }

    // Returns all background color names
    public function getBackgroundColors() {
        return array_keys($this->background_colors);
    }
}
class ConsoleLogLevel {
    const DEBUG=2;
    const INFO=4;
    const WARN = 6;
    const ERROR=8;
    private static $map ;
private static function init(){
    self::$map[self::DEBUG]="" ;
    self::$map[self::INFO]="green" ;
    self::$map[self::ERROR]="red" ;
    self::$map[self::WARN]="yellow" ;
}
    public static  function getLevelColor($level){
        if(empty(self::$map)){
            self::init();
        }
        return self::$map[$level];
    }
}
class ConsoleLog
{
    /**
     * @var Colors
     */
    private  $colors;
    private $level ;
    public  function  __construct($level=ConsoleLogLevel::DEBUG){
        $this->colors= new Colors;
        $this->level = $level ;

    }
    private function error_log($msg){
         $fd=fopen('php://stderr','a');
            fwrite($fd,$msg);
            fclose($fd);
    }
    public function  log($level=ConsoleLogLevel::INFO,$message,...$args){
        if(defined("IN_DEBUG")){
           $message =  "CurrentThreadId:".\Thread::getCurrentThreadId()." , ".sprintf($message,...$args);
        }else{
            $message =sprintf($message,...$args);
        }
        $this->error_log($this->colors->getColoredString($message.PHP_EOL,ConsoleLogLevel::getLevelColor($level)));

    }
    public function info($message,...$args){
        if($this->level <=ConsoleLogLevel::INFO)
        $this->log(ConsoleLogLevel::INFO,$message,...$args);
    }
    public function debug($message,...$args){
        if($this->level <=ConsoleLogLevel::DEBUG)
            $this->log(ConsoleLogLevel::DEBUG,$message,...$args);
    }
    public function error($message,...$args){
        if($this->level <=ConsoleLogLevel::ERROR)
            $this->log(ConsoleLogLevel::ERROR,$message,...$args);
    }
    public function normal($message,...$args){
            $this->log(ConsoleLogLevel::DEBUG,$message,...$args);
    }
    public function warn($message,...$args){
        if($this->level <=ConsoleLogLevel::WARN)
            $this->log(ConsoleLogLevel::WARN,$message,...$args);
    }
}
